package cn.quang.search.toolkit.component.items.impl;

import cn.quang.search.toolkit.component.AbstractSearchItem;
import cn.quang.search.toolkit.component.constants.Constants;
import cn.quang.search.toolkit.enums.DataSourceTypeEnum;
import cn.quang.search.toolkit.enums.SearchOperator;
import cn.quang.search.toolkit.provider.entity.KeyOperatorValue;
import cn.quang.search.toolkit.provider.entity.QuerySlice;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class RangeSearchItem extends AbstractSearchItem {

    private final Collection<?> values;

    private final static SearchOperator[] VALID_OPERATORS = new SearchOperator[]{
            SearchOperator.In,
            SearchOperator.NotIn
    };

    protected <T> RangeSearchItem(DataSourceTypeEnum dsType, String col, SearchOperator operator, Collection<T> values){
        super(dsType, col, operator);
        if(CollectionUtils.isEmpty(values)){
            throw new IllegalArgumentException(operator + "必须提供参数");
        }
        if(!isValidOperator(operator)){
            throw new IllegalArgumentException("操作符[" + operator + "]不在ESRangeSearchItem的允许范围内！");
        }
        this.values = values;
    }

    @SafeVarargs
    @SuppressWarnings("unchecked")
    protected <T> RangeSearchItem(DataSourceTypeEnum dsType, String col, SearchOperator operator, T... valueArray){
        super(dsType, col, operator);

        Collection<T> values = new ArrayList<>();

        if(valueArray.length > 1){
            values = Arrays.asList(valueArray);
        }else{
            T val = valueArray[0];
            if(val.getClass().isArray()){
                T[] v = (T[]) val;
                values = Arrays.asList(v);
            }else{
                values.add(val);
            }
        }

        this.values = values;
    }

    @Override
    public QuerySlice getQuerySlice() {

        String operatorString = operator.getOperator();

        int size = values.size();
        boolean needComma = false;

        StringBuilder sbSql = new StringBuilder(Constants.FIELD);
        sbSql.append(" ")
                .append(Constants.OPERATOR)
                .append(" (");
        for(int i=0;i<size;i++){
            if(needComma){
                sbSql.append(",");
            }
            else {
                needComma = true;
            }

            sbSql.append(Constants.VALUE);
        }

        sbSql.append(")");

        String sqlTemplate = sbSql.toString();

        return QuerySlice.of(
                sqlTemplate,
                KeyOperatorValue.of(col, Collections.singletonList(col), Collections.singletonList(operatorString), new ArrayList<>(values))
        );
    }

    /**
     * 判断操作符是否在当前允许范围内
     * @param operator 操作符
     */
    private boolean isValidOperator(SearchOperator operator){
        for(SearchOperator allowedOperator : VALID_OPERATORS){
            if(allowedOperator == operator){
                return true;
            }
        }

        return false;
    }

}
